package ctrlLayer;

import java.util.ArrayList;

import modelLayer.Customer;
import modelLayer.Product;
import modelLayer.SalesOrder;
import modelLayer.SalesLine;
import dbLayer.DbSalesOrder;

public class CtrOrder {

	private DbSalesOrder dbSo;
	private CtrBPartner ctrbp;
	private CtrProduct ctrp;
	private CtrInvoice ctri;
	
	public CtrOrder() {
		dbSo = new DbSalesOrder();
		ctrbp = new CtrBPartner();
		ctrp = new CtrProduct();
		ctri = new CtrInvoice();
	}

	/**
	 * Method for creating a SalesOrder object, and inserting it in the databbase.
	 * @param customerId The ID of the customer making the order.
	 * @return The SalesOrder object created and inserted in the database, used for referencing and visual presentation purposes.
	 */
	public SalesOrder createOrder(int customerId) {
		SalesOrder soObj = new SalesOrder(ctrbp.retrieveCustomer(customerId));
		try {
			dbSo.insertOrderInDb(soObj);
			ArrayList<SalesOrder> soList = dbSo.retrieveAllOrders();
			int val = soList.size() - 1;
			soObj = soList.get(val);
		} catch(Exception e) {
			System.out.println("Error in createOrder in CtrOrder");
		}
		return soObj;
	}
	
	/**
	 * Method for adding a product to the order.
	 * @param productId The specific product to be added to the order.
	 * @param quantity The quantity of products to be added to the order.
	 * @param orderId The order the SalesLine is to be added to.
	 * @return The order, which now contains an extra salesLine.
	 */
	public SalesOrder addProduct(int productId,int quantity,int orderId){
		Product pObj = ctrp.retrieveProduct(productId);
		SalesOrder soObj = dbSo.retrieveOrderFromDb(orderId);
		soObj.addProduct(pObj, quantity);
		try {
			dbSo.updateOrder(soObj);
		} catch (Exception e) {
			System.out.println("Error in addProduct in CtrOrder");
		}
		return soObj;
	}
	
	/**
	 * Method for adding a order to a customers invoice.
	 * @param orderId The orderid, matching the order to be added to the invoice.
	 * @return The order matching the ID.
	 */
	public SalesOrder invoicing(int orderId){
		SalesOrder soObj = dbSo.retrieveOrderFromDb(orderId);
		ctri.addOrderToInvoice(soObj);
		try {
			dbSo.updateOrder(soObj);
		} catch (Exception e) {
		}
		return soObj;
	}
	
	/**
	 * Method for retrieving all the orders in the DB
	 * @return A list containing all the dbs orders.
	 */
	public ArrayList<SalesOrder> retrieveAllOrders(){
		return dbSo.retrieveAllOrders();
	}
	
	/**
	 * Method for retrieving a specific order from the db.
	 * @param orderId The orderId of the wanted order.
	 * @return The wanted order.
	 */
	public SalesOrder retrieveOrderFromDb(int orderId){
		return dbSo.retrieveOrderFromDb(orderId);
	}
	
	/**
	 * Method for retrieving all order associated to a customer.
	 * @param customerId The customer, which orders are wanted.
	 * @return A list of the customers orders.
	 */
	public ArrayList<SalesOrder> retrieveByCustomer(int customerId){
		return dbSo.retrieveByCustomer(customerId);
	}
	
	/**
	 * Method for getting a customer to the order UI, that needs to be assigned to the order.
	 * @param customerId
	 * @return
	 */
	public Customer retrieveCustomerById(int customerId){
		return ctrbp.retrieveCustomer(customerId);
	}
	
	public double getTotalOrderPrice(int orderId){
		double val = 0;
		SalesOrder soObj = retrieveOrderFromDb(orderId);
		ArrayList<SalesLine> slList = (ArrayList<SalesLine>) soObj.getSalesLines();
		for(SalesLine sl: slList){
			val = val + (sl.getQuantity()*sl.getProduct().getSalesPrice());
		}
		return val;
	}
	
	public ArrayList<Product> getAllProducts(){
		return ctrp.retrieveAllProducts();
	}
	
	public Product getProductById(int productId){
		return ctrp.retrieveProduct(productId);
	}
	
}
